package com.have.blog.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.config.hloder.RequestHolder;
import com.have.blog.model.mapper.UserFeedbackMapper;
import com.have.blog.model.mapper.UserMapper;
import com.have.blog.model.models.User;
import com.have.blog.model.models.UserFeedback;
import com.have.blog.model.models.UserFeedbackExample;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.web.constant.BlogConstant;
import com.have.blog.web.dto.FeedbackDto;
import com.have.blog.web.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@SuppressWarnings("all")
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    private UserFeedbackMapper feedbackMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<FeedbackDto> getUserFeedbackList() {
        Integer userUid = RequestHolder.getUserUid();
        PageHelper.startPage(1, 20);
        UserFeedbackExample example = new UserFeedbackExample();
        example.createCriteria()
                .andUserUidEqualTo(userUid)
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("create_time desc");
        List<UserFeedback> feedbackList = feedbackMapper.selectByExample(example);
        return MyBeanUtils.copyListProperties(feedbackList, FeedbackDto::new);
    }

    @Override
    public Wrapper addFeedback(FeedbackDto feedbackDto) {
        Integer userUid = RequestHolder.getUserUid();
        User user = userMapper.selectByPrimaryKey(userUid);
        if (user == null || user.getCommentStatus() == BlogConstant.ZERO) {
            return Wrapper.unauthorized();
        }
        // TODO 邮件反馈
        UserFeedback feedback = new UserFeedback();
        MyBeanUtils.copyProperties(feedbackDto, feedback);
        feedback.setStatus(BlogStatus.ENABLE.getStatus());
        feedback.setFeedbackStatus(0);
        feedback.setUserUid(userUid);
        int result = feedbackMapper.insert(feedback);
        if (result > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }
}
